//
//  FourthViewController.m
//  ContainerDemo
//
//  Created by qianfeng on 15/3/3.
//  Copyright (c) 2015年 WeiZhenLiu. All rights reserved.
//

#import "FourthViewController.h"

@interface FourthViewController ()
{
    UIScrollView * _scrollView;
}
@end

@implementation FourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
NSLog(@"viewDidLoad");
    
    /**
     *   /**
     *   // buttons
     UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
     [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
     [button setTitle:_titles[i] forState:UIControlStateNormal];
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     
     // set button frame
     static CGFloat originX = 0;
     CGRect frame = CGRectMake(originX+padding, 0, button.intrinsicContentSize.width, kTopbarHeight);
     button.frame = frame;
     originX      = CGRectGetMaxX(frame) + padding; //originX + padding + button.intrinsicContentSize.width;
     
     [self addSubview:button];
     [self.buttons addObject:button];
     
     */
    
    
    
    self.view.backgroundColor = [UIColor yellowColor];
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = CGRectMake(0, 40, self.view.bounds.size.width, 20);
    NSArray * array = @[@"one",@"nsdafas",@"asdfdsa",@"dsafs",@"dsafdsafdsadfds",@"1121313131",@"1231",@"dfsafsaf",@"dfsdafdsafdsadf",@"dfdsafds"];
    
   static CGFloat oringin = 0.0;
    for (NSInteger i= 0; i<10; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.frame = CGRectMake(oringin, 0, button.intrinsicContentSize.width, 20);
        oringin= CGRectGetMaxX(button.frame)+20;
        [_scrollView addSubview:button];
       
    }
    _scrollView.contentSize = CGSizeMake(CGRectGetMaxX([[_scrollView.subviews lastObject] frame]), 20);
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100,50);
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"移动" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
}
- (void)btnClick:(UIButton *)buttton{
    static NSInteger curentpath = 0;
    
    [self setCurrentPage:curentpath];
    curentpath++;
}
- (void)setCurrentPage:(NSInteger)curentPage{
    UIButton * button = (UIButton *)[_scrollView.subviews objectAtIndex:curentPage];
    
    [_scrollView scrollRectToVisible:button.frame animated:YES];
}
@end
